﻿namespace Yyyg.WebUI
{
    public static class RSASingle
    {
        static int keysize = 512;

        /// <summary>
        /// 公钥
        /// </summary>
        public static string PubKey_PEM_PKCS1 { get; private set; }

        /// <summary>
        /// 私钥
        /// </summary>
        public static string PrvKey_PEM_PKCS1 { get; private set; }

        /// <summary>
        /// 模数位数
        /// </summary>
        public static int KeySize
        {
            get
            {
                return keysize;
            }
            set
            {
                if (value == keysize) return;
                keysize = value;
                Create(value);
            }
        }

        static RSASingle()
        {
            Create(KeySize);
        }

        static void Create(int size)
        {
            var tup = RSACryptoHelper.CreateKey_PEM_PKCS1(size);
            PrvKey_PEM_PKCS1 = tup.Item1;
            PubKey_PEM_PKCS1 = tup.Item2;
        }

        /// <summary>
        /// 对数据进行解密
        /// </summary>
        /// <param name="cipher">密文</param>
        /// <returns>明文</returns>
        public static string Decrypt(string cipher)
        {
            return RSACryptoHelper.Decrypt_PEMKey(cipher, PrvKey_PEM_PKCS1);
        }
    }
}